package atguigu.gson;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class Demo3 {
    public static void main(String[] args) {

//        UserInfo user = new UserInfo("yejing",34);
//        String s = simpleObjToJson(user);
//        System.out.println(s); // {"name":"yejing","age":34}


//        String json = "{\"name\":\"yejing\",\"age\":34}";
//        UserInfo user1 = simpleJsonToObj(json, UserInfo.class);
//        System.out.println(user1.getAge());
//        System.out.println(user1.getName());

//        UserInfo user = new UserInfo("yejing",34);
//        Job job = new Job("程序员","上海易立德");
//        Worker worker = new Worker();
//        worker.setName("张三");
//        worker.setAge(45);
//        worker.setJob(job);
//        List<String> list = Arrays.asList("张三", "法外狂徒", "传奇人物");
//        worker.setNickNames(list);
//
//        String s = complexObjToJson(worker);
//        System.out.println(s); // {"job":{"jobName":"程序员","company":"上海易立德"},"nickNames":["张三","法外狂徒","传奇人物"],"name":"张三","age":45}

//        String str = "{\"job\":{\"jobName\":\"程序员\",\"company\":\"上海易立德\"},\"nickNames\":[\"张三\",\"法外狂徒\",\"传奇人物\"],\"name\":\"张三\",\"age\":45}";
//        Worker worker = complexJsonToObj(str, Worker.class);
//        System.out.println(worker.getName());
//        System.out.println(worker.getAge());
//        System.out.println(worker.getNickNames());


//        List<String> strings = Arrays.asList("张三","李四","王五");
//        Gson gson = new Gson();
//        String s = gson.toJson(strings);
//        System.out.println(s);

//        String str = "[\"张三\",\"李四\",\"王五\"]";
//        Gson gson = new Gson();
//        String[] nameArray = gson.fromJson(str, String[].class);
//        for (int i = 0; i < nameArray.length; i++) {
//            System.out.println(nameArray[i]);
//        }

//        List<String> list = new ArrayList<>();
//        list.add("zhangsan");
//        list.add("lisi");
//        list.add("wangwu");
//        String json = listToJson(list);
//        System.out.println(json); // ["zhangsan","lisi","wangwu"]


//        String json = "[{\"id\":1,\"name\":\"zhangsan\",\"age\":18},{\"id\":2,\"name\":\"sili\",\"age\":28}]";
//        System.out.println(jsonToList(json, User.class));

//        String json = "{\"id\":1,\"login_name\":\"张三\",\"age\":18}";
//        User user = complexJsonToObj(json, User.class);
//        System.out.println(user);

        User user = new User(1, "张三", 18);
        System.out.println(complexObjToJson(user));
    }

    public static <T> T jsonToList(String json,Class<T> cls) {
        if (Objects.isNull(json)) return null;
        try {
            Gson gson = new Gson();
            // 需要注意这里的type
            Type type = new TypeToken<ArrayList<T>>(){}.getType();
            return gson.fromJson(json, type);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String listToJson(List list) {
        if (Objects.isNull(list)) return "";
        try {
            Gson gson = new Gson();
            return gson.toJson(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static <T> T complexJsonToObj(String json, Class<T> cls) {
        Gson gson = new Gson();
        if (Objects.isNull(json)) return null;
        T obj = gson.fromJson(json, cls);
        if (Objects.isNull(obj)) {
            return null;
        } else {
            return obj;
        }
    }


    public static String complexObjToJson(Object obj) {
        if (Objects.isNull(obj)) return "";
        try {
            Gson gson = new Gson();
            return gson.toJson(obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static <T> T simpleJsonToObj(String json, Class<T> cls) {
        Gson gson = new Gson();
        if (Objects.isNull(json)) return null;
        T obj = gson.fromJson(json, cls);
        if (Objects.isNull(obj)) {
            return null;
        } else {
            return obj;
        }
    }

    public static String simpleObjToJson(Object obj) {
        if (Objects.isNull(obj)) return "";
        try {
            Gson gson = new Gson();
            return gson.toJson(obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}
